﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace TweetMemeNET
{
    /// <summary>
    /// Represents a single tweet
    /// </summary>
    public class Tweet
    {
        /// <summary>
        /// Instantiates a new Tweet object based on an XML string
        /// </summary>
        /// <param name="xml"></param>
        public Tweet(string xml)
        {
            XElement e = XElement.Parse(xml);
            
            Populate(e);
        }

        /// <summary>
        /// Instantiates a new Tweet object based on an XElement object
        /// </summary>
        /// <param name="tweetElement"></param>
        public Tweet(XElement tweetElement)
        {
            Populate(tweetElement);
        }

        protected void Populate(XElement tweetElement)
        {
            // Parse story details
            ID=tweetElement.Element(XName.Get("id", string.Empty)).Value;
            Created=DateTime.Parse(ID=tweetElement.Element(XName.Get("created_at", string.Empty)).Value);
            Text=tweetElement.Element(XName.Get("tweet", string.Empty)).Value;

            // Parse user details
            XElement userElement = tweetElement.Element(XName.Get("user", string.Empty));
            User = new User(userElement);
        }

        /// <summary>
        /// Gets the ID of the tweet
        /// </summary>
        public string ID { get; protected set; }

        /// <summary>
        /// Gets the date and time of the tweet
        /// </summary>
        public DateTime Created { get; protected set; }

        /// <summary>
        /// Gets the actual tweet text
        /// </summary>
        public string Text { get; protected set; }

        /// <summary>
        /// Gets the username of the Twitter user
        /// </summary>
        public User User { get; protected set; }
    }
}
